#!/bin/bash
#
# /etc/rc.shutdown
#

. /etc/rc.conf
. /etc/rc.d/functions

# avoid staircase effect
stty onlcr

echo " "
printhl "Initiating Shutdown..."
echo " "

run_hook shutdown_start

[[ -x /etc/rc.local.shutdown ]] && /etc/rc.local.shutdown

stop_all_daemons

stat_busy "Saving Random Seed"
	RANDOM_SEED=/var/lib/misc/random-seed
	install -TDm 0600 /dev/null $RANDOM_SEED
	POOL_FILE=/proc/sys/kernel/random/poolsize
	if [[ -r $POOL_FILE ]]; then
		read POOL_SIZE < $POOL_FILE
		(( POOL_SIZE /= 8 ))
	else
		POOL_SIZE=512
	fi
	dd if=/dev/urandom of=$RANDOM_SEED count=1 bs=$POOL_SIZE &>/dev/null
stat_done

[[ $TIMEZONE ]] && status "Configuring Time Zone" set_timezone "$TIMEZONE"

# Write to wtmp file before unmounting
halt -w

# stop monitoring of lvm2 groups before unmounting filesystems
[[ $USELVM = [Yy][Ee][Ss] && -x $(type -P lvm) ]] &&
	status "Deactivating monitoring of LVM2 groups" vgchange --monitor n

# any future uevents can and should be ignored
#dbg:dlin status "Shutting down UDev" udevadm control --exit

run_hook shutdown_prekillall

kill_all

run_hook shutdown_postkillall

run_hook shutdown_preumount

# unmount any non-api partitions that are backed by swap, we don't want to
# move their contents into memory (waste of time and might caues OOM).
status "Unmounting Swap-backed Filesystems" umount_all "tmpfs"

# almost everything is dead now, so the swap should hopefully be relatively
# empty, and quick to switch off
status "Deactivating Swap" swapoff -a

status "Unmounting Non-API Filesystems" umount_all

run_hook shutdown_postumount

# Kill non-root encrypted partition mappings
if [[ -f /etc/crypttab && $CS ]] && grep -q ^[^#] /etc/crypttab; then
	stat_busy "Deactivating encrypted volumes:"
		# Maybe someone has LVM on an encrypted block device
		# executing an extra vgchange is errorless
		[[ $USELVM = [Yy][Ee][Ss] ]] && vgchange --sysinit -a n &>/dev/null
		do_lock() {
			stat_append "${1}.."
			if $CS remove "$1" &>/dev/null; then
				stat_append "ok "
			else
				stat_append "failed "
			fi
		}
		read_crypttab do_lock
	stat_done
fi

[[ $USELVM = [Yy][Ee][Ss] && -x $(type -P lvm) && -d /sys/block ]] &&
	status "Deactivating LVM2 groups" vgchange --sysinit -a n &>/dev/null

run_hook shutdown_poweroff

if [[ -x /run/initramfs/shutdown ]]; then

	# decide what we want to do
	if [[ $RUNLEVEL = 0 ]]; then
		action="poweroff"
	else
		action="reboot"
	fi

	# make /run/initrafs a mount
	mount --bind /run/initramfs /run/initramfs

	# in case someone has shared our mountpoints, unshare them
	mount --make-private /run/initramfs
	mount --make-private /

	# bind all api mounts
	mkdir -p /run/initramfs/{sys,proc,dev,run,oldroot}
	mount --bind /sys /run/initramfs/sys
	mount --bind /proc /run/initramfs/proc
	mount --bind /dev /run/initramfs/dev
	mount --bind /run /run/initramfs/run

	# enter shutdownramfs
	cd /run/initramfs
	pivot_root . oldroot

	#reexec init
	/oldroot/sbin/init u

	# run /shutdown in the new root
	exec chroot . /shutdown $action </dev/console >/dev/console 2>&1

else

	status "Remounting Root Filesystem Read-only" \
		mount -n -o remount,ro /

	# Power off or reboot
	printsep
	if [[ $RUNLEVEL = 0 ]]; then
		printhl "${C_H2}POWER OFF"
		poweroff -d -f -h -i
	else
		printhl "${C_H2}REBOOTING"
		# if kexec is installed and a kernel is loaded, use it
		[[ -x $(type -P kexec) ]] && kexec -e &>/dev/null
		reboot -d -f -i
	fi

fi

# End of file
# vim: set ts=2 sw=2 noet:
